const axios=require("axios")

const yuque_token="ZW9aH3CUDFWvC0ZU6nzoozBg03smn6Y5D36LqB8x"
const text_template_map={
    "模板1":"https://langjie.yuque.com/vdbwag/hf58w4/pl6z16",
    "模板2":"https://langjie.yuque.com/vdbwag/hf58w4/spmqk9",
/////////////////////////////////////////////////////////////////////////////////
    "贺卡模板1":"https://langjie.yuque.com/vdbwag/hf58w4/zrwb3g",
    "贺卡模板2":"https://langjie.yuque.com/vdbwag/hf58w4/klpwmp",
    "贺卡模板3":"https://langjie.yuque.com/vdbwag/hf58w4/ipsge8",
    "贺卡模板4":"https://langjie.yuque.com/vdbwag/hf58w4/ekvy1r"
}


const yuqueUtils={}
yuqueUtils.enableCache=true;

yuqueUtils.cacheData={}

/**
 * 原始内容
 */
yuqueUtils.getYuquePureContent= async function (template_key){
    let template_url= text_template_map[template_key];
    let all_path=template_url.split("https://langjie.yuque.com")[1];
    let all_path_arr=all_path.split("/");
    let yuqueApi=`https://langjie.yuque.com/api/v2/repos/${all_path_arr[1]}/${all_path_arr[2]}/docs/${all_path_arr[3]}`;
    let response = await axios({
        method:'get',
        url:yuqueApi,
        headers: {"X-Auth-Token": yuque_token,"User-Agent":"PostmanRuntime/7.26.1"},
        withCredentials: true})
    let artileContent=response.data.data.body
    return artileContent;
}



/**
 * 语雀文章
 */
yuqueUtils.getYuqueArtile= async function (template_key){
    if(yuqueUtils.cacheData[template_key] && yuqueUtils.enableCache){
        return yuqueUtils.cacheData[template_key];
    }
    let artileContent=await yuqueUtils.getYuquePureContent(template_key);
    artileContent=artileContent.replace(/\s+/g,'')
    artileContent=artileContent.replace("```html","")
    artileContent=artileContent.replace("```","")
    yuqueUtils.cacheData[template_key]=artileContent;
    return artileContent;

}

/**
 * 语雀贺卡
 */
yuqueUtils.greetingCard= async function (template_key){
    if(yuqueUtils.cacheData[template_key] && yuqueUtils.enableCache){
        return yuqueUtils.cacheData[template_key];
    }
    let artileContent=await yuqueUtils.getYuquePureContent(template_key);
    let s=artileContent.replace(/\s+/g,'');
    let bgImg= /#背景图片\[(.+?)\]/.exec(s)[1].trim()
    let content=/#文本```html(.*)```/.exec(s)[1].trim()
    return {bgImg,content};
}



module.exports = yuqueUtils;
